'use strict';

const { generateSnowflakeId } = require('../../app/utils/snowflake');

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    // 生成字典类型ID
    const sysVisibleDictId = generateSnowflakeId();
    const sysStatusDictId = generateSnowflakeId();
    const imageCategoryDictId = generateSnowflakeId();
    const imageTagDictId = generateSnowflakeId();
    
    // 插入字典类型数据
    const dictTypes = [
      {
        id: sysVisibleDictId,
        name: '系统可见',
        code: 'sys_visible',
        description: '系统中可见隐藏参数',
        status: true,
        create_time: new Date('2025-08-18T07:04:55.000Z'),
        update_time: new Date('2025-08-18T07:04:55.000Z'),
      },
      {
        id: sysStatusDictId,
        name: '系统状态',
        code: 'sys_status',
        description: '系统中的所有状态',
        status: true,
        create_time: new Date('2025-08-18T06:59:14.000Z'),
        update_time: new Date('2025-08-18T06:59:14.000Z'),
      },
      {
        id: imageCategoryDictId,
        name: '图片分类',
        code: 'image_category',
        description: '图片管理模块使用',
        status: true,
        create_time: new Date('2025-08-18T06:46:59.000Z'),
        update_time: new Date('2025-08-18T06:46:59.000Z'),
      },
      {
        id: imageTagDictId,
        name: '图片标签',
        code: 'image_tag',
        description: '图片管理模块使用',
        status: true,
        create_time: new Date('2025-08-18T06:46:42.000Z'),
        update_time: new Date('2025-08-18T06:46:42.000Z'),
      },
    ];

    await queryInterface.bulkInsert('sys_dict_types', dictTypes);

    // 获取插入的字典类型ID
    const sysVisibleDict = sysVisibleDictId;
    const sysStatusDict = sysStatusDictId;
    const imageCategoryDict = imageCategoryDictId;
    const imageTagDict = imageTagDictId;

    // 生成字典项ID
    const hotTagId = generateSnowflakeId();
    const avatarTagId = generateSnowflakeId();
    const enableStatusId = generateSnowflakeId();
    const disableStatusId = generateSnowflakeId();
    const hiddenVisibleId = generateSnowflakeId();
    const showVisibleId = generateSnowflakeId();
    
    // 插入字典项数据
    const dictItems = [
      // 图片标签字典项
      {
        id: hotTagId,
        dict_type_id: imageTagDict,
        label: '热门',
        value: '1',
        sort: 0,
        status: true,
        create_time: new Date('2025-08-18T06:47:41.000Z'),
        update_time: new Date('2025-08-18T06:47:41.000Z'),
      },
      {
        id: avatarTagId,
        dict_type_id: imageTagDict,
        label: '头像',
        value: '2',
        sort: 0,
        status: true,
        create_time: new Date('2025-08-18T06:47:24.000Z'),
        update_time: new Date('2025-08-18T06:47:24.000Z'),
      },
      // 系统状态字典项
      {
        id: enableStatusId,
        dict_type_id: sysStatusDict,
        label: '启用',
        value: '1',
        sort: 0,
        status: true,
        create_time: new Date('2025-08-18T06:59:36.000Z'),
        update_time: new Date('2025-08-18T06:59:36.000Z'),
      },
      {
        id: disableStatusId,
        dict_type_id: sysStatusDict,
        label: '禁用',
        value: '0',
        sort: 0,
        status: true,
        create_time: new Date('2025-08-18T06:59:47.000Z'),
        update_time: new Date('2025-08-18T06:59:47.000Z'),
      },
      // 系统可见字典项
      {
        id: hiddenVisibleId,
        dict_type_id: sysVisibleDict,
        label: '隐藏',
        value: '0',
        sort: 0,
        status: true,
        create_time: new Date('2025-08-18T07:05:29.000Z'),
        update_time: new Date('2025-08-18T07:05:29.000Z'),
      },
      {
        id: showVisibleId,
        dict_type_id: sysVisibleDict,
        label: '显示',
        value: '1',
        sort: 1,
        status: true,
        create_time: new Date('2025-08-18T07:05:20.000Z'),
        update_time: new Date('2025-08-18T07:05:20.000Z'),
      },
    ];

    await queryInterface.bulkInsert('sys_dict_items', dictItems);
  },

  async down(queryInterface, Sequelize) {
    // 删除字典项数据
    await queryInterface.bulkDelete('sys_dict_items', null, {});
    // 删除字典类型数据
    await queryInterface.bulkDelete('sys_dict_types', null, {});
  },
};